<?php

namespace App\Http\Requests\Api;

class VerificationCodeRequest extends FormRequest
{
    public function rules()
    {
        $rules = [
            'user_mobile' => [
                'required',
                'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/',
            ],
        ];

        switch ($this->method()) {
            case 'POST':
                $rules = array_merge($rules, ['type' => 'required|in:register,binding,login,reset_password']);
                if (in_array($this->type, ['register', 'binding'])) {
                    array_push($rules['user_mobile'], 'unique:ucenter_user_info_detail');
                } elseif (in_array($this->type, ['login', 'reset_password'])) {
                    array_push($rules['user_mobile'], 'exists:ucenter_user_info_detail');
                }
                return $rules;
            case 'PUT':
            case 'PATCH':
            case 'GET':
            case 'DELETE':
            default:
                return [];
        }
    }

    public function messages()
    {
        return [
            'user_mobile.required' => '手机号不能为空',
            'user_mobile.regex' => '手机号不合法',
            'user_mobile.unique' => '手机号已存在',
            'user_mobile.exists' => '手机号不存在',
            'type.required' => '类型不能为空',
            'type.in' => '类型只能为 register|binding|login|reset_password',
            'sms_key.required' => '验证码标识不能为空',
            'sms_code.required' => '验证码不能为空',
        ];
    }
}
